﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using ZHDJ.Core;

namespace ZHDJ.Data.Models.Center
{
    /// <summary>
    /// 阵地管理发布表
    /// </summary>
    public class PartyPossyInfo : EntityBase
    {
        #region 属性


        /// <summary>
        /// 阵地名称
        /// </summary>
        
        public string Name { get; set; }

        /// <summary>
        /// 所属组织
        /// </summary>
        
        public string BranchID { get; set; }

        /// <summary>
        /// 地址
        /// </summary>
        
        public string Address { get; set; }

        /// <summary>
        /// 交通
        /// </summary>
        
        public string Traffic { get; set; }

        /// <summary>
        /// 介绍
        /// </summary>
        
        public string Describe { get; set; }

        /// <summary>
        /// 是否显示在首页
        /// </summary>
        public bool IsShowPortal { get; set; }
        
        /// <summary>
        /// 联系电话
        /// </summary>
        public string LinkPhone { get; set; }

        /// <summary>
        /// 接待时间， 1：星期一  2：星期二  如此类推    多个用“,”分隔
        /// </summary>
        public string VisitDays { get; set; }


        /// <summary>
        /// 阵地管理员
        /// </summary>
        public string PossyAdmin { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public string ModifyBy { get; set; }
        #endregion

        #region 查询管理
        public static DataSet GetGridData(int pageIndex, int pageSize, string isShowPortal, string branchid, string searchKey = "")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };


            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  a.*, 
                        b.BranchName,
                        if(a.IsShowPortal=1,'已发布','草稿') as IsShowPortalName
                        from tblPartyPossyInfo a
                        left join tblpartyorgbranch b on (b.branchid=a.branchid)
                        where 1=1 ");
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and a.Name like ?key ");
                parameters.Add(
                    new MySqlParameter(){DbType = DbType.String,ParameterName = "key",Value = string.Format("%{0}%", searchKey)});
            }
            if (!string.IsNullOrEmpty(isShowPortal))
            {
                sb.Append(" and a.IsShowPortal=?IsShowPortal");
                parameters.Add(new MySqlParameter(){DbType = DbType.Int32,ParameterName = "IsShowPortal", Value = isShowPortal });
            }
            if (!string.IsNullOrEmpty(branchid))
            {
                sb.Append(" and a.branchid in (select ChildBranchID from tblpartyorgbranchrelation brr where (brr.branchid=?branchid)) ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "branchid", Value = branchid });
            }
            sb.Append(" order by IsShowPortal desc, a.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
        #endregion


    }
}
